package com.itheima.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.itheima.jsonresult.JsonResult;
import com.itheima.oss.Ossproperites;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Api(tags = "公共模块")
@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {

    @Autowired
    private Ossproperites ossproperites;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "文件",required = true,dataType = "Object"),
    })
    @ApiOperation(value = "上传文件")
    @PostMapping("/upload")
    public JsonResult upload(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossproperites.getEndpoint(), ossproperites.getAccessKey(), ossproperites.getSecret());
        try {
            ossClient.putObject(ossproperites.getBucketName(), fileName, new ByteArrayInputStream(file.getBytes()));
        } catch (Exception oe) {
            log.error("上传文件异常");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        // http://oss-cn-hangzhou.aliyuncs.com/a.jpg
        // https://linying-reggie.oss-cn-hangzhou.aliyuncs.com/1525189943-38523.png
        return new JsonResult(1, "上传成功", ossproperites.getUrl() + "/" + fileName,null);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "图片名称",required = true,dataType = "String"),
    })
    @ApiOperation(value = "下载图片")
    @GetMapping("/download")
    public JsonResult download(@RequestParam String name) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossproperites.getEndpoint(), ossproperites.getAccessKey(), ossproperites.getSecret());
        try {
            // 调用ossClient.getObject返回一个OSSObject实例，该实例包含文件内容及文件元信息。
            OSSObject ossObject = ossClient.getObject(ossproperites.getBucketName(), name);
            // 调用ossObject.getObjectContent获取文件输入流，可读取此输入流获取其内容。
            InputStream content = ossObject.getObjectContent();
            if (content != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                while (true) {
                    String line = reader.readLine();
                    if (line == null) break;
                    System.out.println("\n" + line);
                }
                // 数据读取完成后，获取的流必须关闭，否则会造成连接泄漏，导致请求无连接可用，程序无法正常工作。
                content.close();
            }
        } catch (Exception oe) {
            log.error("下载文件异常");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return new JsonResult(1, "下载成功", ossproperites.getUrl() + "/" + name,null);
    }
}
